package com.zaofan.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.zaofan.bean.OpenArea;
import com.zaofan.bean.OpenCity;
import com.zaofan.constant.StateConstant;
import com.zaofan.dao.OpenAreaDao;
import com.zaofan.dao.ShopDao;

public class OpenAreaService {
	private OpenAreaDao OpenAreaDao = new OpenAreaDao();

	public void delete(OpenArea openArea) {
		OpenAreaDao.del(openArea);
	}

	public void add(OpenArea openArea) {
		openArea.setAddTime(new Date());
		openArea.setBuilderNum(0);
		openArea.setBuilderNum(0);
		openArea.setShopNum(0);
		OpenCityService cityService = new OpenCityService();
		OpenCity city = cityService.findById(openArea.getCityId());
		openArea.setCityName(city.getCityName());
		OpenAreaDao.add(openArea);
		city.setAreaNum(city.getAreaNum() + 1);
		cityService.update(city);
	}

	public void updateWithName(OpenArea openArea) {
		OpenCityService cityService = new OpenCityService();
		OpenCity city = cityService.findById(openArea.getCityId());
		openArea.setCityName(city.getCityName());
		OpenAreaDao.update(openArea);
	}

	public void update(OpenArea openArea) {
		OpenCityService cityService = new OpenCityService();
		OpenCity city = cityService.findById(openArea.getCityId());
		openArea.setCityName(city.getCityName());
		OpenAreaDao.update(openArea);
		OpenBuilderService builderService = new OpenBuilderService();
		builderService.updateAreaName(openArea);
		ShopDao shopService = new ShopDao();
		shopService.updateAreaName(openArea);
	}

	public List<OpenArea> findAll(Map<String, Object> params) {
		List<OpenArea> data = OpenAreaDao.findAll(params);
		if (data == null) {
			data = new ArrayList<>();
		}
		return data;
	}

	public Long count(Map<String, Object> params) {
		return OpenAreaDao.count(params);
	}

	public OpenArea findById(Integer id) {
		Map<String, Object> params = new HashMap<>();
		params.put("id", id);
		return OpenAreaDao.findOne(params);
	}

	public List<OpenArea> findByCityId(Integer cityId) {
		Map<String, Object> params = new HashMap<>();
		params.put("cityId", cityId);
		return OpenAreaDao.findAll(params);
	}

	public void updateCityName(OpenCity city) {
		OpenAreaDao.updateCityName(city);
	}

	public void setAreaState(OpenArea area) {
		OpenAreaDao.updateState(area);
		OpenCityService cityService = new OpenCityService();
		OpenCity city = cityService.findById(area.getCityId());
		if (area.getState() == StateConstant.STATE_SHOW) {
			city.setAreaNum(city.getAreaNum() + 1);
		} else {
//			OpenBuilderService builderService = new OpenBuilderService();
//			builderService.updateStateUnShowByAreaId(area.getId());
			city.setAreaNum(city.getAreaNum() - 1);
		}
		cityService.update(city);
	}

	public List<OpenArea> findOpenByCityId(int cityId) {
		Map<String, Object> params = new HashMap<>();
		params.put("cityId", cityId);
		params.put("state", StateConstant.STATE_SHOW);
		return OpenAreaDao.findAll(params);
	}

	public void updateStateUnShowByCityId(int cityId) {
		OpenAreaDao.updateStateUnShowByCityId(cityId);
	}

}
